name: Publish

on:
  release:
    types: [ published ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2.3.4

      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          distribution: 'zulu'
          java-version: 17.0.x

      - name: Set up GPG
        run:
          echo -n "${GPG_KEY_CONTENTS}" | base64 --decode > ${GITHUB_WORKSPACE}/${GPG_KEY_ID}.gpg
          gpg --quiet --batch --yes --decrypt --passphrase="${{secrets.SECRET_PASSPHRASE}}" \
          --output ${GITHUB_WORKSPACE}/secret.gpg ${GITHUB_WORKSPACE}/${GPG_KEY_ID}.gpg
        env:
          GPG_KEY_ID: ${{ secrets.GPG_KEY_ID }}
          GPG_KEY_CONTENTS: ${{ secrets.GPG_KEY_CONTENTS }}

      - name: "Gradle Build"
        run: ./gradlew build -Pversion=${GITHUB_REF:10}

      - name: "Gradle Publish"
        run: |
          ./gradlew publishToSonatype -Pversion=${GITHUB_REF:10} \
                    -Psigning.keyId=${GPG_KEY_ID} \
                    -Psigning.password=${GPG_PASSPHRASE} \
                    -Psigning.secretKeyRingFile=${GITHUB_WORKSPACE}/secret.gpg
        env:
          ORG_GRADLE_PROJECT_sonatypeUsername: ${{ secrets.OSSRH_USERNAME }}
          ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.OSSRH_PASSWORD }}
          GPG_KEY_ID: ${{ secrets.GPG_KEY_ID }}
          GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
